package com.itheima.demo;

import com.itheima.domain.Classes;
import com.itheima.domain.Student;
import com.itheima.mapper.One2ManyMapper;
import com.itheima.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;


import java.io.IOException;
import java.util.List;

public class One2ManyDemo {
    //创建工具类对象
    public static MapperUtils mapperUtils = new MapperUtils();

    public static void main(String[] args) throws IOException {
        //通过工具类获取SqlSession对象
        SqlSession sqlSession = mapperUtils.getSqlSession("MyBatisConfig.xml");
        //获取映射对象
        One2ManyMapper mapper = sqlSession.getMapper(One2ManyMapper.class);
        //调用方法
        List<Classes> classes = mapper.findClasses();
        System.out.println("======================");
        for (Classes c : classes) {
            System.out.println("班级: " + c.getName());
            List<Student> stuList = c.getStuList();
            System.out.print("\t学生:");
            for (Student student : stuList) {
                System.out.print(student.getName()+"\t");
            }
            System.out.println();
        }
        System.out.println("======================");
        //释放资源
        mapperUtils.closeResource();
    }
}
